import { Body, Controller, Get, Param, ParseIntPipe, Post, Req } from '@nestjs/common';
import { UserService } from './user.service';
import { ReqAttrs } from '../../constant';
import { ResponseUtilService } from '@app/response-util';
import { DtoPipe } from '../../pipe/dto/dto.pipe';
import { UpdateInfoDto } from './dto/update.info.dto';
import ChangePassword from './dto/changePassword.dto';

@Controller('user')
export class UserController {
    constructor(
        private readonly service:UserService
    ) {}


    @Post('setpwd')
    async setpwd(@Body('password') password:string, @Req() req) {
        try {
            const r = await this.service.setPassword(req[ReqAttrs.USER_ID], password)
            return r ? ResponseUtilService.successRes() : ResponseUtilService.failRes()
        } catch (error) {
            return ResponseUtilService.failRes(error.message)
        }
    }

     @Post('updatebaseinfo')
    async updatebaseinfo(@Body(new DtoPipe()) dto:UpdateInfoDto, @Req() req:ReqAttrs) {
        try {
            const r =  await this.service.updateBaseInfo(dto, req[ReqAttrs.USER_ID])
            return r ? ResponseUtilService.successRes(r) : ResponseUtilService.failRes()
        } catch (error) {
            return ResponseUtilService.failRes(error.message)
        }
    }

    @Post('changePwd')
        async changePwd(@Body(new DtoPipe()) dto:ChangePassword, @Req() req) {
        try {
            const r = await this.service.changePwd(req[ReqAttrs.USER_ID], dto.oldpassword, dto.password)
            return r ? ResponseUtilService.successRes() : ResponseUtilService.failRes()
        } catch (error) {
            return ResponseUtilService.failRes(error.message)
        }
    }

    @Post('addfriend')
    async addfriend(@Body('accountId') accountId:string, @Req() req) {
        this.service.addFriend(req[ReqAttrs.USER_ID], accountId)
    }

    @Post('logoff')
    async logoff(@Req() req) {
        this.service.logoff(req[ReqAttrs.USER_ID])
    }

    @Post('report')
    async report(@Req() req) {
        return ResponseUtilService.successRes()
    }

}
